package com.umeng.analytics.pro;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Base64;

import com.umeng.analytics.AnalyticsConfig;

import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class bc {
    private static final int a = 1;
    private static final int b = 2;
    private static final int c = 3;
    private ad d;
    private af e;
    private final int f;
    private static Context g;
    private be h;
    private aw i;
    private JSONObject j;
    private boolean k;
    private boolean l;

    public bc(Context arg3, be arg4) {

        this.f = 1;
        this.k = false;
        this.d = ad.a(arg3);
        this.e = af.a(arg3);
        bc.g = arg3;
        this.h = arg4;
        this.i = new aw(arg3);
        this.i.a(this.h);
    }

    public void a(az arg2) {
        this.e.a(arg2);
    }

    public void a() {
        int v1_1;
        long v2;
        SharedPreferences v0_1;
        long v8 = -1;
        try {
            if (this.j != null) {
                this.c();
            } else {
                this.b();
            }
        } catch (Throwable v0) {
        }

        try {
            if (!bv.k(bc.g)) {
                return;
            }

            v0_1 = ba.a(bc.g);
            if (v0_1 == null) {
                this.i.a();
                return;
            }

            String v1 = v0_1.getString("uopdta", "");
            v2 = q.b(System.currentTimeMillis());
            if (!TextUtils.isEmpty(((CharSequence) v1))) {
                return;
            }

            long v4 = v0_1.getLong("uopdte", -1);
            v1_1 = v0_1.getInt("uopcnt", 0);
            if (v4 == v8) {
                v0_1.edit().putInt("uopcnt", v1_1 + 1).commit();
                this.i.a();
            } else {
                if (v2 != v4) {
                    try {
                        v0_1.edit().putInt("uopcnt", 1).commit();
                        this.i.a();
                    } catch (Throwable v0) {
                        return;
                    }
                } else {
                    if (v1_1 < 2) {
                        v0_1.edit().putInt("uopcnt", v1_1 + 1).commit();
                        this.i.a();
                    }
                }
            }
            v0_1.edit().putLong("uopdte", v2).commit();
            return;
        } catch (Throwable v0) {
            return;
        }
    }

    public void a(JSONObject arg1) {
        this.j = arg1;
    }

    public void a(boolean arg1) {
        this.k = arg1;
    }

    static aw a(bc arg1) {
        return arg1.i;
    }

    static int a(bc arg1, byte[] arg2) {
        return arg1.a(arg2);
    }

    private int a(byte[] arg5) {
        bp v0 = new bp();
        cj v1 = new cj(new cy.a());
        try {
            v1.a(((cg) v0), arg5);
            if (v0.a == 1) {
                this.e.b(v0.i());
                this.e.d();
            }

            by.c("send log:" + v0.f());
        } catch (Throwable v1_1) {
        }

        int v0_1 = v0.a == 1 ? 2 : 3;
        return v0_1;
    }

    public void b(boolean arg1) {
        this.l = arg1;
    }

    static boolean b(bc arg1) {
        return arg1.l;
    }

    private void b() {
        cc.a(bc.g).i().a(new cc.b() {
            public void a(File arg1) {
            }

            public boolean b(File arg5) {
                InputStream v2_3 = null;
                int v2_2;
                byte[] v3_1;
                FileInputStream v2;
                boolean v0 = true;
                InputStream v3 = null;
                try {
                    v2 = new FileInputStream(arg5);
                    v3_1 = bw.b(((InputStream) v2));
                    bw.c(((InputStream) v2));
                    byte[] v2_1 = bc.a(bc.this).a(v3_1);
                    if (v2_1 == null) {
                        v2_2 = 1;
                    } else {
                        v2_2 = bc.a(bc.this, v2_1);
                    }
                    if (!bc.b(bc.this)) {
                        if (v2_2 == 1) {
                            v0 = false;
                        }
                        return v0;
                    }
                    return v0;
                } catch (Exception v0_2) {
                    return false;
                } finally {
                    bw.c(v2_3);
                }
            }

            public void c(File arg2) {
                bc.c(bc.this).k();
            }
        });
    }

    static be c(bc arg1) {
        return arg1.h;
    }

    private void c() {
        try {
            this.d.a();
            try {
                String v0_2 = Base64.encodeToString(new cp().a(this.d.b()), 0);
                if (TextUtils.isEmpty(((CharSequence) v0_2))) {
                } else {
                    JSONObject v1 = this.j.getJSONObject("header");
                    v1.put("id_tracking", v0_2);
                    this.j.put("header", v1);
                }
            } catch (Exception v0_1) {
            }
        } catch (Throwable v0) {
            return;
        }

        try {
            byte[] v0_3 = String.valueOf(this.j).getBytes();
            if (v0_3 == null) {
                return;
            }
            if (bt.a(bc.g, v0_3)) {
                return;
            }
            aa v0_4 = !this.k ? aa.a(bc.g, AnalyticsConfig.getAppkey(bc.g), v0_3) : aa.b(bc.g, AnalyticsConfig.getAppkey(bc.g), v0_3);
            byte[] v1_1 = v0_4.c();
            cc.a(bc.g).g();
            v0_3 = this.i.a(v1_1);
            int v0_5 = v0_3 == null ? 1 : this.a(v0_3);
            switch (v0_5) {
                case 1: {
                    if (this.l) {
                    } else {
                        cc.a(bc.g).a(v1_1);
                    }
                    return;
                }
                case 2: {
                    this.d.d();
                    this.h.k();
                    return;
                }
                case 3: {
                    this.h.k();
                    return;
                }
            }
        } catch (Throwable v0) {
            return;
        }
    }
}

